import { defineStore } from "pinia";

/**
 * 工作流运行状态管理
 */
export const useFlowRunStore = defineStore({
  id: "airag-flow-run",
  
  // 状态定义
  state: () => ({
    status: "",           // 工作流状态: running, finished, failed
    inputParams: {},      // 输入参数
    outputParams: {},     // 输出参数
    nodeSteps: [],        // 节点步骤记录
    time: 0,              // 总运行时间(毫秒)
    timeText: "",         // 格式化的运行时间
    beginTime: 0,         // 开始时间戳
    resMessage: ""        // 响应消息
  }),
  
  // 计算属性
  getters: {
    isRunning() {
      return this.status === "running";
    },
    isFinished() {
      return this.status === "finished" || this.status === "failed";
    },
    isFailed() {
      return this.status === "failed";
    }
  },
  
  // 操作方法
  actions: {
    /**
     * 初始化状态
     */
    initStatus() {
      this.time = 0;
      this.timeText = "";
      this.status = "";
      this.nodeSteps = [];
      this.inputParams = {};
      this.outputParams = {};
      this.resMessage = "";
      this.expansion=false;
    },
    
    /**
     * 开始工作流
     * @param {Object} params - 输入参数
     */
    start(params) {
      this.initStatus();
      this.status = "running";
      this.beginTime = Date.now();
      this.inputParams = params;
          console.log("inputParams")
      console.log(this.inputParams)
    },
    
    /**
     * 完成工作流
     * @param {boolean} checkNodeStatus - 是否检查节点状态
     * @param {string} message - 响应消息
     * @param {Object} outputParams - 输出参数
     */
    finish(checkNodeStatus, message, outputParams) {
      // 检查是否有失败的节点
      const hasFailedNode = checkNodeStatus 
        ? this.nodeSteps.some((step) => step.status === "fail") 
        : true;
      
      // 设置最终状态
      this.status = hasFailedNode ? "failed" : "finished";
      
      // 计算运行时间
      this.time = Date.now() - this.beginTime;
      this.timeText = `${(this.time / 1000).toFixed(3)}s`;
      
      // 保存输出参数和消息
      this.outputParams = outputParams;
        
      this.resMessage = message;
    },
    
    /**
     * 结束工作流
     */
    end() {
      this.initStatus();
    },
    
    /**
     * 添加节点步骤
     * @param {Object} step - 步骤信息
     */
    addStep(step) {
      this.nodeSteps.push({
        ...step,
        time: 0,
        timeText: "",
        beginTime: Date.now(),
        expansion: false
      });
    },
    
    /**
     * 更新节点步骤状态
     * @param {string} nodeId - 节点ID
     * @param {string} status - 状态: success, fail
     * @param {Object} outputParams - 输出参数
     */
    updateStepStatus(nodeId, status, outputParams) {
      const step = this.nodeSteps.find((step) => step.node.id === nodeId);
      
      if (step) {
        step.status = status;
        step.outputParams = outputParams;
        
        // 如果步骤完成，计算运行时间
        if (status === "success" || status === "fail") {
          step.time = Date.now() - step.beginTime;
          step.timeText = `${(step.time / 1000).toFixed(3)}s`;
        }
      }
    },
    
    /**
     * 获取节点步骤
     * @param {string} nodeId - 节点ID
     * @returns {Object|null} 步骤信息
     */
    getStep(nodeId) {
      return this.nodeSteps.find((step) => step.node.id === nodeId);
    }
  }
});
